package model
{
	import flash.data.SQLStatement;
	
	
	public class Supplier
	{
		private var _pk:uint;
		private var _id:String;
		private var _name:String;
		private var _tin:String;
		private var _address:String;
		private var _contact:String;
		private var _contact_number:String;
		private var _s_columns:String = "";
		private var _s_values:String;
		private var _s_insert_statement:String;
		private var _s_delete_statement:String;
		private var _a_parameters:Array;
		
		public function Supplier()
		{
			//TODO: implement function
			_a_parameters = new Array();
		}
		
		/**
		 * setter for pk column
		*/ 
		public function set pk(u_pk:uint):void { 
			_pk = u_pk;
			_a_parameters[":pk"] = u_pk;
		}
		
		/**
		 * getter for pk column
		*/
		public function get pk(): uint {
			return _pk;
		}
		
		/**
		 * setter for id column
		*/ 
		public function set id(s_id:String):void { 
			_a_parameters[":id"] = _id = s_id;			
		}
		
		/**
		 * getter for id column
		*/
		public function get id(): String {
			return _id;
		}
		
		/**
		 * setter for name column
		*/ 
		public function set name(s_name:String):void { 
			_a_parameters[":name"] = _name = s_name;
		}
		
		/**
		 * getter for name column
		*/
		public function get name(): String {
			return _name;
		}
		
		/**
		 * setter for tin column
		*/ 
		public function set tin(s_tin:String):void { 
			_a_parameters[":tin"] = _tin = s_tin;
		}
		
		/**
		 * getter for tin column
		*/
		public function get tin(): String {
			return _tin;
		}
		
		/**
		 * setter for address column
		*/ 
		public function set address(s_address:String):void { 
			_a_parameters[":address"] = _address = s_address;
		}
		
		/**
		 * getter for address column
		*/
		public function get address(): String {
			return _address;
		}
		
		/**
		 * setter for contact column
		*/ 
		public function set contact(s_contact:String):void { 
			_a_parameters[":contact"] = _contact = s_contact; 
		}
		
		/**
		 * getter for contact column
		*/
		public function get contact(): String {
			return _contact;
		}
		
		/**
		 * setter for contact_number column
		*/ 
		public function set contact_number(s_contact_number:String):void { 
			_a_parameters[":contact_number"] = _contact_number = s_contact_number;
		}
		
		/**
		 * getter for contact_number column
		*/
		public function get contact_number(): String {
			return _contact_number;
		}
		
		/**
		 * prepare strings for insert statements
		*/
		private function func_concatenate_insertion():void {
			_s_columns = _s_values = "";
			if (_pk > 0) func_add_column("pk");
			
			if (_id != "") func_add_column("id");
			
			if (_name != "") func_add_column("name");
		
			if (_tin != "") func_add_column("tin");
			
			if (_address != "") func_add_column("address");
			
			if (_contact != "") func_add_column("contact");
			
			if (_contact_number != "") func_add_column("contact_number");
			
			var pattern:RegExp = /,/gi;
			_s_values = _s_columns.replace(pattern, ",:");
			_s_values = ":" + _s_values;
		}
		
		/**
		 * sets s_columns
		*/
		private function func_add_column(str_value:String):void {
			if (_s_columns.length > 0) _s_columns += "," + str_value;
			else _s_columns = str_value;
		}
		
		/**
		 * getter for s_columns
		*/
		private function get s_columns():String {
			return _s_columns;
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_insert_statement():String {
			func_concatenate_insertion();
			return "INSERT INTO suppliers(" + _s_columns + ") VALUES(" + _s_values + ")";
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_delete_statement():String {
			_a_parameters = new Array();
			_a_parameters[":pk"] = this._pk;
			return "DELETE FROM suppliers WHERE pk = :pk";
		}
		
		/**
		 * getter for s_update_statement column
		*/
		public function get s_update_statement():String {
			return "UPDATE suppliers " + 
					"SET " + 
						"id = :id, name = :name, tin = :tin, address = :address, " + 
						"contact = :contact, contact_number = :contact_number " + 
					"WHERE pk = :pk";
		}
		/**
		 * getter for s_parameters
		*/
		public function func_set_sqlstatement(sql:SQLStatement, s_statement:String):void {
			sql.text = s_statement;
			for (var col:* in _a_parameters) {
				sql.parameters[col] = _a_parameters[col];
			}
		}
	}
}